package org.huangrui.spark.java.core.rdd.instance;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;

import java.util.Arrays;
import java.util.List;

/**
 * 内存数据是如何进行存储分配
 * @Author hr
 * @Create 2024-10-16 10:55
 */
public class Spark02_RDD_Memory_Partition_Data {
    public static void main(String[] args) {
        SparkConf conf = new SparkConf().setMaster("local[*]").setAppName("sparkCore");
        JavaSparkContext jsc = new JavaSparkContext(conf);

        List<Integer> list = Arrays.asList(1, 2, 3, 4, 5, 6);
         /*
          【1】
          【2，3】
          【4】
          【5，6】
          -------------------------------
          len=6, partnum=4
          (0 until 4) => [0, 1, 2, 3]
          0 => ((i * length) / numSlices, (((i + 1) * length) / numSlices))
            => ((0 * 6) / 4, (((0 + 1) * 6) / 4))
            => (0, 1) => 1
          1 => ((i * length) / numSlices, (((i + 1) * length) / numSlices))
            => ((1 * 6) / 4, (((2) * 6) / 4))
            => (1, 3) => 2
          2 => ((i * length) / numSlices, (((i + 1) * length) / numSlices))
            => ((2 * 6) / 4, (((3) * 6) / 4))
            => (3, 4) => 1
          3 => ((i * length) / numSlices, (((i + 1) * length) / numSlices))
            => ((3 * 6) / 4, (((4) * 6) / 4))
            => (4, 6) => 2
         */
        // TODO Spark分区数据的存储基本原则：平均分
        JavaRDD<Integer> rdd = jsc.parallelize(list,4);
        rdd.saveAsTextFile("output");

        jsc.close();
    }
}
